<?php

App::uses('AppController', 'Controller');

class StoresController extends AppController {
    
    public $uses = array('Store', 'User');
    
    public $title = 'Prodavnice';

    public $paginate = array(
        'limit'  => self::ITEMS_PER_PAGE,
        'order'  => 'Store.name'
    );
    
    public function index() {
        $stores = $this->paginate('Store');
        $this->set(compact('stores'));
    }
    
    protected function setCommonData() {
        $res = $this->User->find('all', array(
            'fields'    => array('User.id', 'User.name', 'User.username'),
            'order'     => 'username',
            'recursive' => 0
        ));
        $allUsers = array();
        foreach($res as $row) {
            $id       = $row['User']['id'];
            $name     = $row['User']['name'];
            $username = $row['User']['username'];
            
            $allUsers[$id] = empty($name) ? $username : $name;
        }
        $this->set(compact('allUsers'));
    }
    
    public function create() {
        $this->setCommonData();
        $this->set('data', $this->Store->create());
        $this->render('form');
    }
    
    public function edit($id) {
        $this->setCommonData();
        $this->set('data', $this->Store->findById($id));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            $this->Store->create();
            $this->Store->set($data);
            
            $toupload = false;
            if (! $data['Store']['logo_file']['error']) {
                $toupload = $data['Store']['logo_file'];
                $data['Store']['logo'] = $toupload['name'];
            }
            unset($data['Store']['logo_file']);
            
            if ($this->Store->validates()) {
                $storeId = $data['Store']['id'];
                if ($storeId > 0) {
                    $this->Store->UsersStore->deleteAll(array(
                        'store_id' => $storeId
                    ));
                }
                if ($this->Store->saveAll($data)) {
                    if ($toupload) {
                        $dest = WWW_ROOT . 'img' . DS . 'stores' . DS . $toupload['name'];
                        move_uploaded_file($toupload['tmp_name'], $dest);
                    }
                
                    $this->flashOk('Prodavnica je sačuvana.');
                    $this->redirect('/stores/edit/' . $this->Store->id);
                } else {
                    $this->flashError('Uneti podaci nisu validni.');
                    $this->set('data', $data);
                    $this->setCommonData();
                    return $this->render('form');
                }
            } else {
                $this->flashError('Uneti podaci nisu validni.');
                $this->set('data', $data);
                $this->setCommonData();
                return $this->render('form');
            }
            
            Configure::write('debug', 2);
            debug($data);die;
        }
        $this->redirect('index');
    }
    
    public function delete($id) {
        if ($id>0) {
            if ($this->Store->delete($id)) {
                $this->flashOk('Prodavnica je obrisana.');
            } else {
                $this->flashError('Prodavnica nije obrisana.');
            }
        }
        $this->redirect('index');
    }

}

?>